package namespace

import (
	"context"
	"kms-backend/config"
	"kms-backend/controllers"
	"kms-backend/utils/logs"

	"github.com/gin-gonic/gin"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func List(r *gin.Context) {
	logs.Debug(nil, "展示命名空间")

	returnData := config.ReturnData{}
	// 生成clientSet, 基础数据basicInfo
	clientSet, basicInfo, err := controllers.BasicInit(r, nil)
	if err != nil {
		returnData.Message = err.Error()
		returnData.Status = 400
		r.JSON(200, returnData)
		return
	}

	// 获取namespace列表
	namespaceList, err := clientSet.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		msg := "获取集群中命名空间失败" + err.Error()
		logs.Error(nil, "集群"+basicInfo.ClusterId+msg)
		returnData.Message = msg
		returnData.Status = 400
	} else {
		msg := "获取集群中命名空间成功"
		logs.Info(nil, "集群"+basicInfo.ClusterId+"中命名空间获取成功")
		returnData.Message = msg
		returnData.Status = 200
		returnData.Data = make(map[string]interface{})
		returnData.Data["items"] = namespaceList.Items
	}
	r.JSON(200, returnData)

}
